package dao.impl;

import bean.Admin;
import dao.AdminDao;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Auther leiHua
 * @Date 2022/5/8
 */
public class AdminDaoImpl implements AdminDao {
    /**
     * 根据name查询
     * @param name
     * @return
     */
    @Override
    public Admin select(String name) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
           conn = JdbcUtils.getConnection();
            ps=conn.prepareStatement("select * from admin where name=?");
            ps.setString(1,name);
            rs= ps.executeQuery();
            Admin admin = new Admin();
            if (rs.next()) {
                String id = rs.getString("id");
                String password=rs.getString(3);
                admin.setName(name);
                admin.setPassword(password);
                admin.setId(id);
                return admin;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
                JdbcUtils.close(conn,ps,rs);
        }
        return null;
    }
}
